package com.leetcode.根据数据结构分类.链表;

import com.leetcode.datastructure.ListNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: xiaomi
 * @date: 2021/2/21
 * @description:
 */
public class A_剑指_Offer06_从尾到头打印链表 {

    /**
     * 哎，我应该使用栈的！
     * @param head
     * @return
     */
    public int[] reversePrint(ListNode head) {
        List<Integer> list = new ArrayList<>();
        ListNode curNode = head;
        while (curNode != null) {
            list.add(curNode.val);
            curNode = curNode.next;
        }
        int len = list.size();
        if (len == 0) {
            return new int[0];
        }

        int[] res = new int[list.size()];
        int sIndex = len - 1;
        for (int i = len - 1; i >= 0; i--) {
            res[i] = list.get(sIndex - i);
        }
        return res;
    }
}
